package com.ktgj.customer.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ktgj.customer.domain.vo.*;
import com.ktgj.hotel.api.RemoteHotelService;
import com.ktgj.hotel.api.domain.DicCityVoApi;
import com.ktgj.hotel.api.domain.DicCountyVoApi;
import com.ktgj.hotel.api.domain.DicProvinceVoApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.customer.mapper.CommonAddressesMapper;
import com.ktgj.customer.domain.CommonAddresses;
import com.ktgj.customer.service.ICommonAddressesService;

/**
 * 常用地址Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-09-01
 */
@Service
public class CommonAddressesServiceImpl implements ICommonAddressesService 
{
    @Autowired
    private CommonAddressesMapper commonAddressesMapper;
    @Autowired
    private RemoteHotelService remoteHotelService;

    /**
     * 查询常用地址
     * 
     * @param primaryKeyId 常用地址ID
     * @return 常用地址
     */
    @Override
    public CommonAddresses selectCommonAddressesById(Long primaryKeyId)
    {
        return commonAddressesMapper.selectCommonAddressesById(primaryKeyId);
    }

    /**
     * 查询常用地址列表
     * 
     * @param commonAddresses 常用地址
     * @return 常用地址
     */
    @Override
    public List<CommonAddresses> selectCommonAddressesList(CommonAddresses commonAddresses)
    {
        return commonAddressesMapper.selectCommonAddressesList(commonAddresses);
    }

    /**
     * 新增常用地址
     * 
     * @param commonAddresses 常用地址
     * @return 结果
     */
    @Override
    public int insertCommonAddresses(CommonAddresses commonAddresses)
    {
        return commonAddressesMapper.insertCommonAddresses(commonAddresses);
    }

    /**
     * 修改常用地址
     * 
     * @param commonAddresses 常用地址
     * @return 结果
     */
    @Override
    public int updateCommonAddresses(CommonAddresses commonAddresses)
    {
        return commonAddressesMapper.updateCommonAddresses(commonAddresses);
    }

    /**
     * 批量删除常用地址
     * 
     * @param primaryKeyIds 需要删除的常用地址ID
     * @return 结果
     */
    @Override
    public int deleteCommonAddressesByIds(Long[] primaryKeyIds)
    {
        return commonAddressesMapper.deleteCommonAddressesByIds(primaryKeyIds);
    }

    /**
     * 删除常用地址信息
     * 
     * @param primaryKeyId 常用地址ID
     * @return 结果
     */
    @Override
    public int deleteCommonAddressesById(Long primaryKeyId)
    {
        return commonAddressesMapper.deleteCommonAddressesById(primaryKeyId);
    }

    /**
     * 我的-新增常用地址
     * */
    @Override
    public int saveCommonAddressesByCustomerId(CommonAddressesVo commonAddressesVo) {
        CommonAddresses commonAddresses = new CommonAddresses();
        commonAddresses.setConsignee(commonAddressesVo.getConsignee());
        commonAddresses.setPhone(commonAddressesVo.getPhone());
        commonAddresses.setRegion(commonAddressesVo.getRegion());
        commonAddresses.setDetailedAddress(commonAddressesVo.getDetailedAddress());
        commonAddresses.setLabel(commonAddressesVo.getLabel());
        commonAddresses.setIsDeletes("false");
        boolean boo = boo(commonAddressesVo.getMemberId());
        if (boo){
            commonAddresses.setWhetherDefaultAddress("true");
        }
        else {
            commonAddresses.setWhetherDefaultAddress("false");
        }

        commonAddresses.setMemberId(commonAddressesVo.getMemberId());
        int count = commonAddressesMapper.insertCommonAddressesByCustomerId(commonAddresses);
        return count;
    }

    /**
     * 查询地区列表
     * */
    public List<ProvincialVo> areaList(){
        List<DicCityVoApi> dicCityVoApiList = remoteHotelService.allDicCityList();//市
        List<DicCountyVoApi> dicCountyVoApiList = remoteHotelService.allDicCountyList();//县
        List<DicProvinceVoApi> dicProvinceVoApiList = remoteHotelService.allDicProvince();//省
        List<ProvincialVo> provincialVoList = new ArrayList<>();

        for (DicProvinceVoApi dicProvinceVoApi : dicProvinceVoApiList) {//遍历省信息
            if (!dicProvinceVoApi.getProvinceName().equals("市辖区")){//去掉市辖区数据
                ProvincialVo provincialVo = new ProvincialVo();
                provincialVo.setId(dicProvinceVoApi.getId());
                provincialVo.setProvinceName(dicProvinceVoApi.getProvinceName());

                List<CitiesVo> citiesVoList = new ArrayList<>();

                for (DicCityVoApi dicCityVoApi : dicCityVoApiList) {//遍历市信息

                    if (dicCityVoApi.getProvinceId().equals(dicProvinceVoApi.getId()) && !dicCityVoApi.getCityName().equals("市辖区")){
                        CitiesVo citiesVo = new CitiesVo();
                        citiesVo.setId(dicCityVoApi.getId());
                        citiesVo.setProvinceId(dicCityVoApi.getProvinceId());
                        citiesVo.setCityName(dicCityVoApi.getCityName());

                        List<CountiesVo> countiesVoList = new ArrayList<>();

                        for (DicCountyVoApi dicCountyVoApi : dicCountyVoApiList) {//遍历县、区信息

                            if (dicCountyVoApi.getCityId().equals(dicCityVoApi.getId()) && !dicCountyVoApi.getCountyName().equals("市辖区")){
                                CountiesVo countiesVo = new CountiesVo();
                                countiesVo.setId(dicCountyVoApi.getId());
                                countiesVo.setCityId(dicCountyVoApi.getCityId());
                                countiesVo.setCountyName(dicCountyVoApi.getCountyName());
                                countiesVoList.add(countiesVo);
                            }

                        }

                        citiesVo.setCountiesVos(countiesVoList);
                        citiesVoList.add(citiesVo);
                    }

                }

                provincialVo.setCitiesVoList(citiesVoList);
                provincialVoList.add(provincialVo);
            }
        }

        return provincialVoList;
    }

    /**
     * 我的-修改常用地址
     * */
    @Override
    public int updateCommonAddressesByCustomerId(CommonAddresses commonAddresses) throws Exception {
        if (commonAddresses != null && !commonAddresses.equals("")){
            boo(commonAddresses.getMemberId());
            return commonAddressesMapper.updateCommonAddressesByCustomerId(commonAddresses);
        }
        else {
            throw new Exception("参数错误");
        }
    }

    /**
     * 查询待编辑的常用地址
     * */
    @Override
    public CommonAddresses addressToBeModified(Long commonAddresserId) {
        return commonAddressesMapper.addressToBeModified(commonAddresserId);
    }

    /**
     * 根据会员id查询常用地址列表
     * */
    @Override
    public List<CAddressVo> addressList(Long memberId) {
        if (memberId > 0){
            List<CommonAddresses> commonAddressesList = commonAddressesMapper.selectCommonaddress(memberId);
            if (commonAddressesList != null){
                List<CAddressVo> cAddressVoList = new ArrayList<>();
                for (CommonAddresses commonAddresses : commonAddressesList) {
                    if (commonAddresses.getIsDeletes().equals("false")){
                        CAddressVo cAddressVo = new CAddressVo();
                        cAddressVo.setCommonAddresserId(commonAddresses.getCommonAddresserId());
                        cAddressVo.setConsignee(commonAddresses.getConsignee());
                        cAddressVo.setPhone(commonAddresses.getPhone());
                        cAddressVo.setLabel(commonAddresses.getLabel());
                        cAddressVo.setWhetherDefaultAddress(commonAddresses.getWhetherDefaultAddress());
                        String region = deleteString(commonAddresses.getRegion());
                        cAddressVo.setRegion(region+commonAddresses.getDetailedAddress());
                        cAddressVoList.add(cAddressVo);
                    }
                }
                return cAddressVoList;
            }
            else {
                return null;
            }
        }
        else {
            return null;
        }
    }

    /**
     * 后端——查询常用地址
     * */
    @Override
    public List<CommonAddresses> backSelectCommonAddresses(Long memberId) throws Exception {
        if (memberId != null && memberId != 0){
            List<CommonAddresses> commonAddressesList = commonAddressesMapper.backSelectCommonAddresses(memberId);
            if (commonAddressesList != null && commonAddressesList.size() > 0){
                List<CommonAddresses> commonAddresses = new ArrayList<>();
                for (CommonAddresses addresses : commonAddressesList) {
                    if (addresses.getIsDeletes().equals("false")){
                        commonAddresses.add(addresses);
                    }
                }
                return commonAddresses;
            }
            else {
                throw new Exception("未查到数据");
            }
        }
        else {
            throw new Exception("缺少必要参数");
        }
    }

    /**
     * 后端——查询待修改的常用地址
     * */
    @Override
    public CommonAddresses backSelectCommonAddressesById(Long commonAddresserId) throws Exception {
        if (commonAddresserId != null && commonAddresserId != 0){
            CommonAddresses commonAddresses = commonAddressesMapper.backSelectCommonAddressesById(commonAddresserId);
            if (commonAddresses != null){
                return commonAddresses;
            }
            else {
                throw new Exception("未查到数据");
            }
        }
        else {
            throw new Exception("缺少必要参数");
        }
    }

    /**
     * 后端——新增常用地址
     * */
    @Override
    public int backInsertCommonAddresses(CommonAddresses commonAddresses) throws Exception {
        if (commonAddresses != null){
            int i = commonAddressesMapper.backInsertCommonAddresses(commonAddresses);
            if (i > 0){
                return 1;
            }
            else {
                throw new Exception("新增失败");
            }
        }
        else {
            throw new Exception("缺少必要参数");
        }
    }
    /**
     * 后端——修改常用地址
     * */
    @Override
    public int backUpdateCommonAddresses(CommonAddresses commonAddresses) throws Exception {
        if (commonAddresses != null){
            int i = commonAddressesMapper.backUpdateCommonAddresses(commonAddresses);
            if (i > 0){
                return 1;
            }
            else {
                throw new Exception("修改失败");
            }
        }
        else {
            throw new Exception("缺少必要参数");
        }
    }

    /**
     * 后端——删除常用地址
     * */
    @Override
    public int backDeleteCommonAddresses(Long commonAddresserId) throws Exception {
        if (commonAddresserId != null && commonAddresserId != 0){
            int i = commonAddressesMapper.backDeleteCommonAddresses(commonAddresserId);
            if (i > 0){
                return 1;
            }
            else {
                throw new Exception("删除失败");
            }
        }
        else {
            throw new Exception("缺少必要参数");
        }
    }

    /**
     * 常用地址认证
     * */
    public boolean boo(Long memberId){
        List<CommonAddresses> commonAddressesList = commonAddressesMapper.selectCommonaddress(memberId);
        boolean flat = false;
        for (CommonAddresses addresses : commonAddressesList) {
            if (addresses.getWhetherDefaultAddress().equals("true")){
                CommonAddresses commonAddresses = new CommonAddresses();
                commonAddresses.setCommonAddresserId(addresses.getCommonAddresserId());
                commonAddresses.setWhetherDefaultAddress("false");
                commonAddressesMapper.updateCommonAddressesByWhetherDefaultAddress(commonAddresses);
                flat = true;
                break;
            }
        }
        return flat;
    }

    /**
     * 去除指定字符
     * */
    public static String deleteString(String str){
        String delStr = "";
        char delChar = '/';
        for (int i = 0; i < str.length(); i++) {
            if(str.charAt(i) != delChar){
                delStr += str.charAt(i);
            }
        }
        return delStr;
    }
}
